var baseUrl = "https://laohumk.cn/laohu/index.php/Api/";
const { regeneratorRuntime } = global
function doPost(url, params){
  return new Promise((resolve,reject)=>{
    params = params || '';
    console.log('传入参数'+JSON.stringify(params))
    wx.showLoading({
      title: '请稍后...',
    });
    wx.request({
      url: baseUrl + url,
      data: params,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (data) {
        console.log('获取数据:' + JSON.stringify(data))
        resolve(data) 
      },
      fail: function (err) {
        reject(err)
      },
      complete: function () {
        wx.hideLoading();
      }
    }); 
  }) 
}


var lotteryBaseUrl = "https://laohumk.cn/lottery/index.php/Api/";
function lotteryDoPost(url, params) {
  return new Promise((resolve, reject) => {
    params = params || '';
    console.log('抽奖传入参数' + JSON.stringify(params))
    wx.showLoading({
      title: '请稍后...',
    }); 
    wx.request({
      url: lotteryBaseUrl + url,
      data: params,
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (data) {
        console.log('获取数据:' + JSON.stringify(data))
        resolve(data)
      },
      fail: function (err) {
        reject(err)
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  })
}


const promisify = fn => {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        resolve(res)
      }

      obj.fail = function (res) {
        reject(res)
      }

      fn(obj)
    })
  }
}


// function promisify(api){
//   return (options, ...params) => {
//     return new Promise((resolve, reject) => {
//       const extras = {
//         success: resolve,
//         fail: reject
//       }
//       api({ options, ...extras }, params)
//     })
//   }
// }

function leftTimer(time) {
  var leftTime = (new Date(time)) - (new Date()); //计算剩余的毫秒数 
  if (leftTime<=0){
    return { hours:'00', minutes:'00', seconds:'00' }
  }else{
    var hours = parseInt(leftTime / 1000 / 60 / 60, 10); //计算剩余的小时 
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟 
    var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数 
    hours = checkTime(hours);
    minutes = checkTime(minutes);
    seconds = checkTime(seconds);
    return { hours, minutes, seconds }
  }
}

function checkTime(i) { //将0-9的数字前面加上0，例1变为01 
  if (i < 10) {
    i = "0" + i;
  }
  return i;
} 

function getTime(t){
  let time = new Date(t*1000); 
  return time.getFullYear() + '-' + checkTime(String((time.getMonth() + 1))) + '-' + checkTime(String(time.getDate()))
}


function accredit(accreditName,callback){
  wx.getSetting({  // 获取用户设置
    success(res) {
      console.log(res)
      if (!res.authSetting[accreditName]) {  // 如果用户之前拒绝了授权
        wx.openSetting({
          success(tag) {
            if (tag.authSetting[accreditName]) {  // 用户在设置页选择同意授权
              wx.showToast({
                title: '授权成功',
              })
            } else {
              accredit(accreditName, callback)
            }
          }
        });
      } else {   //  用户已经授权
        callback()
      }
    }
  })
}

// 判断是否授权
function isGetUserInfo(href,gotoHref){
  wx.redirectTo({
    url: href + '?href=' + encodeURIComponent(gotoHref),
  }) 
} 
 
// 模态弹框
function showModal(title, content, confirmText, success, fail, showCancel){
  wx.showModal({
    title,
    content,
    confirmText,
    showCancel,
    success: function (res) {
      if (res.confirm) {
        success()
      } else if (res.cancel) {
        fail()
      }
    }
  })
}
// 失败弹框
function showErr(title,duration) {
  wx.showToast({
      title,
      image: '../../../../image/error.png', 
      duration, 
    })
}


module.exports = {
  promisify, doPost, leftTimer, accredit, isGetUserInfo, showModal, showErr, lotteryDoPost, getTime
}    